package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Baoque;
import com.itgaoshu.hospitalproject.bean.Huishou;
import com.itgaoshu.hospitalproject.bean.Record;
import com.itgaoshu.hospitalproject.bean.Ypharmacy;
import com.itgaoshu.hospitalproject.service.PharmacyService;
import com.itgaoshu.hospitalproject.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("pharmacy")
public class PharmacyController {
    @Autowired
    private PharmacyService pharmacyService;
    @Autowired
    private RecordService recordService;

    //查询药品仓库
    @RequestMapping("selectpharmacy")
    @ResponseBody
    public Object selectpharmary(Ypharmacy ypharmacy, Integer page, Integer limit) {
        System.out.print("进去查询药房方法");
        PageHelper.startPage(page, limit);
        List<Ypharmacy> drugstores = pharmacyService.selpharmacy(ypharmacy);
        PageInfo pageInfo = new PageInfo(drugstores);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        drugstoresData.put("count", pageInfo.getTotal());
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }

    //添加一条药品采到报缺单
    @RequestMapping("addbaoque")
    @ResponseBody
    public Integer addbaoque(Baoque baoque) {
        Integer selbaoqueName = pharmacyService.selbaoqueName(baoque);
        if (selbaoqueName == 0) {
            int addbaoque = pharmacyService.addbaoque(baoque);
            return addbaoque;
        } else {
            int upbaoquenum = pharmacyService.upbaoquenum(baoque);
            return upbaoquenum;
        }
    }
    @RequestMapping("delpharymary")
    @ResponseBody
    public Integer delpharymary(Ypharmacy ypharmacy, Huishou huishou) {
        int delpharymacy = pharmacyService.delpharymacy(ypharmacy);
        if (delpharymacy == 1) {
            pharmacyService.addhuishou(huishou);
        }
        System.out.println(delpharymacy);
        return delpharymacy;
    }
    //查询药品仓库
    @RequestMapping("selecthuishou")
    @ResponseBody
    public Object selecthuishou(Huishou huishou, Integer page, Integer limit) {
        System.out.print("进去查询药房方法");
        PageHelper.startPage(page, limit);
        List<Huishou> drugstores = pharmacyService.selhuishou(huishou);
        PageInfo pageInfo = new PageInfo(drugstores);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        drugstoresData.put("count", pageInfo.getTotal());
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }

    @RequestMapping("delhuishou")
    @ResponseBody
    public Integer delhuishou(Huishou huishou, Record record) {
        System.out.println("进入处理");
        int delhuishou = pharmacyService.delhuishou(huishou);
        if (delhuishou == 1) {
           int addjilu = recordService.addjilu(record);
        }
        return delhuishou;

    }

}